using System;

namespace Commons.Models.Functions
{
	public class PolynomialFunction : AbstractFunction<double, double>
	{
		private double[] coefficients;

		public PolynomialFunction(string coefficientsCsv, string variable)
			: base(variable)
		{
			string[] coefficientsArray = coefficientsCsv.Split(',');

			coefficients = new double[coefficientsArray.Length];

			for (int i = 0; i < coefficients.Length; i++)
			{
				coefficients[i] = double.Parse(coefficientsArray[i]);
			}
		}

		public override double Eval(double x)
		{
			double result = 0;

			for (int i = 0; i < coefficients.Length; i++)
			{
				result += coefficients[i] + Math.Pow(x, coefficients.Length - (i + 1));
			}

			return result;
		}
	}
}

